package com.caigou.modules.forum.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.utils.EditTextUtils
import com.caigou.modules.forum.R

/**
 * @author #Suyghur.
 * Created on 2022/02/22
 */
class PacketPublishDialog(context: Context) : Dialog(context) {

    private lateinit var tvBalance: TextView
    private lateinit var etMoney: EditText
    private lateinit var etNum: EditText
    private lateinit var tvMoney: TextView
    lateinit var btnConfirm: Button

    var packetCount = 0
    var packetMoney = 0.00f

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_packet_publish)

        tvBalance = findViewById(R.id.packet_publish_tv_balance)
        tvBalance.text = "余额${AppBackgroundInfo.instance.userBean!!.balance}元"

        etMoney = findViewById(R.id.packet_publish_et_money)
        etMoney.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                val money = s.toString()
                if (TextUtils.isEmpty(money)) {
                    packetMoney = 0.00f
                    tvMoney.text = "0.00元"
                } else {
                    if (EditTextUtils.isNumber(money)) {
                        packetMoney = money.toFloat()
                        tvMoney.text = "${packetMoney}元"
                    } else {
                        packetMoney = 0.00f
                        tvMoney.text = "0.00元"
                    }
                }
            }
        })

        etNum = findViewById(R.id.packet_publish_et_num)
        etNum.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                val num = s.toString()
                packetCount = if (TextUtils.isEmpty(num)) {
                    0
                } else {
                    if (EditTextUtils.isNumber(num)) {
                        num.toInt()
                    } else {
                        0
                    }
                }
            }
        })

        tvMoney = findViewById(R.id.packet_publish_tv_money)

        btnConfirm = findViewById(R.id.packet_publish_btn_confirm)

    }
}